package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.mapper.*;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.utils.HttpClientUtil;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.CannotCreateTransactionException;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    @Transactional
    public void add(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        Long id = dishMapper.addDish(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.stream().forEach(f -> f.setDishId(id));
        if (flavors != null && flavors.size() > 0) {
            dishFlavorMapper.addFlavors(flavors);
        }
    }

    @Override
    public PageResult pageDish(DishPageQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage(), queryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageDish(queryDTO);
        //转为VO层对象
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    @Override
    @Transactional
    public void deleteByids(List<Long> idList) {
        List<Dish> dishes = dishMapper.getbyIds(idList);
        List<Long> ids = new ArrayList<>();
        System.out.println(idList);
        dishes.forEach(dish -> { //排除起售状态
            if (dish.getStatus().equals(StatusConstant.DISABLE)) ids.add(dish.getId());
        });
        //判断是否有套餐绑定
        System.out.println(ids);
        if (ids.size() == 0) {
            throw new BaseException("起售状态");
        }
        List<Long> bandIds = setmealDishMapper.getByids(ids);
        bandIds.forEach(ids::remove);
        if (ids.size() == 0) {
            throw new BaseException("有套餐绑定");
        }
        System.out.println(ids);
        //从dish表删除
        dishMapper.deleteBy(ids);
        //从口味表删除
        dishFlavorMapper.deleteByIds(ids);
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        List<Long> temp = new ArrayList<>();
        temp.add(dishDTO.getId());
        dishFlavorMapper.deleteByIds(temp);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(f -> {
                f.setDishId(dish.getId());
            });
            dishFlavorMapper.addFlavors(flavors);
        }
        dishMapper.update(dish);
    }

    @Override
    public DishVO getByid(Long id) {
        DishVO dishVO = new DishVO();
        Dish dish = dishMapper.getbyId(id);
        BeanUtils.copyProperties(dish, dishVO);
        Category category = categoryMapper.getByid(dishVO.getCategoryId());
        List<DishFlavor> flaByDishID = dishFlavorMapper.getFlaByDishID(dishVO.getId());
        dishVO.setCategoryName(category.getName());
        dishVO.setFlavors(flaByDishID);
        return dishVO;
    }

    @Override
    public void updateStatus(Integer status, Integer id) {
        Dish dish = new Dish();
        dish.setId(id.longValue());
        dish.setStatus(status);
        dishMapper.update(dish);
    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        List<DishVO> dishVOList = (List<DishVO>) valueOperations.get("dish_" + dish.getCategoryId());
        if (dishVOList != null && dishVOList.size() > 0) {
            return dishVOList;
        }
        List<Dish> dishList = dishMapper.getByCatId(dish.getCategoryId());
        dishVOList = new ArrayList<>();
        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);
            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getFlaByDishID(d.getId());
            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }
        redisTemplate.opsForValue().set("dish_" + dish.getCategoryId(), dishVOList);
        return dishVOList;
    }

    @Override
    public List<Dish> getByCid(Long categoryId) {
        return dishMapper.getByCatId(categoryId);
    }

}
